package com.minhua.jijiehao2.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.minhua.jijiehao2.R;
import com.minhua.jijiehao2.base.adapter.ViewHolder;
import com.minhua.jijiehao2.base.adapter.abslistview.BaseCommonAdapter;
import com.minhua.jijiehao2.bean.GridBean;

import java.util.List;

/**
 * @author moo
 * @date 2017/12/4.
 * @describe
 */

public class LoanGridView extends PopupWindow {
    private Context context;
    private List<GridBean> data;

    private ViewHolder mViewHolder;
    private BaseCommonAdapter<GridBean> adapter;


    public LoanGridView(Context context, List<GridBean> data) {
        super(context);
        this.context = context;
        this.data = data;
        initView();
    }


    private void initView() {
        mViewHolder = ViewHolder.get(context, null, null, R.layout.layout_loan_pop, 0);
        initData();
        setContentView(mViewHolder.getConvertView());
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0);
        this.setBackgroundDrawable(dw);
    }

    private void initData() {
        mViewHolder.setOnClickListener(R.id.llMain, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        adapter = new BaseCommonAdapter<GridBean>(context, R.layout.item_loan_pop, data) {
            @Override
            public void convert(final ViewHolder holder, final GridBean gridBean) {
                final TextView textView = holder.getView(R.id.tvName);
                textView.setText(gridBean.getName());
                if (gridBean.isChoosed()) {
                    textView.setBackgroundResource(R.drawable.shape_loan_item_choose);
                    textView.setTextColor(context.getResources().getColor(R.color.mainRed));
                } else {
                    textView.setBackgroundResource(R.drawable.shape_loan_item_nochoose);
                    textView.setTextColor(context.getResources().getColor(R.color.textColorLevel1));
                }
            }
        };
        GridView gvLoan = mViewHolder.getView(R.id.gvLoan);
        gvLoan.setColumnWidth(3);
        gvLoan.setAdapter(adapter);

        gvLoan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GridBean gridBean = data.get(position);
                if (!gridBean.isChoosed()) {
                    for (int i = 0; i < data.size(); i++) {
                        GridBean itemBean = data.get(i);
                        itemBean.setChoosed(false);
                    }
                    gridBean.setChoosed(true);
                    adapter.notifyDataSetChanged();
                    if (limitListener != null) {
                        limitListener.onLimit(position);
                    }
                }
                dismiss();
            }
        });
    }

    public void setData(List<GridBean> data) {
        this.data = data;
    }

    public void notifyDataChanged() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    private OnLimitListener limitListener;

    public void setLimitListener(OnLimitListener limitListener) {
        this.limitListener = limitListener;
    }

    public interface OnLimitListener {
        void onLimit(int index);
    }

}
